/* 
 -----------------------------------------------------------------------------
                   Cogaen - Component-based Game Engine (v3)
 -----------------------------------------------------------------------------
 This software is developed by the Cogaen Development Team. Please have a 
 look at our project home page for further details: http://www.cogaen.org
    
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Copyright (c) 2010 Roman Divotkey

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/

package org.cogaen.java2d;

import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;

public class Camera {
	
	private String name;
	private AffineTransform transform = new AffineTransform();	 
	private double posX = 0.0;
	private double posY = 0.0;
	private double orientation = 0.0;
	private double zoom = 1.0;
	private boolean flipHorizontal = false;
	private int screenWidth;
	private int screenHeight;
	private Viewport viewport;
	private int mask = 0xFFFF;
	
	Camera(String name, int screenWidth, int screenHeight) {
		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
		this.viewport = new Viewport(0, 0, screenWidth, screenHeight);
		recalculateTransform();
	}
	
	public void setMask(int mask) {
		this.mask = mask;
	}
	
	public int getMask() {
		return this.mask;
	}
	
	AffineTransform getTransform() {
		return this.transform;
	}

	public void inverse(Point2D src, Point2D dst) {
		try {
			this.transform.inverseTransform(src, dst);
		} catch (NoninvertibleTransformException e) {
			e.printStackTrace();
		}
	}
	
	
	public int getScreenWidth() {
		return this.screenWidth;
	}
	
	public int getScreenHeight() {
		return this.screenHeight;
	}
	
	public void setViewPort(int x, int y, int width, int height) {
		if (x < 0 || x >= this.screenWidth || y < 0 || y >= this.screenHeight) {
			throw new IllegalArgumentException("upper left corner of " + "" +
					"viewport not within screen boundaries");
		}
		if (width < 0 || width > this.screenWidth || height < 0 
				|| height > this.screenHeight) {
			throw new IllegalArgumentException("view port dimensions not " + 
					"within screen boundaries");			
		}
		
		this.viewport.setUpperLeftPoint(x, y);
		this.viewport.setDimensions(width, height);
		recalculateTransform();
	}
	
	Viewport getViewport() {
		return this.viewport;
	}
	
	public void setPosition(double x, double y) {	
		setPose(x, y, this.orientation);
	}
	
	public void setOrientation(double angle) {
		setPose(this.posX, this.posY, angle);
	}
	
	public void setPose(double x, double y, double angle) {
		// adjust to java2d's coordinate system and flip y-coordinate and angle
		this.posX = x;
		this.posY = -y;
		this.orientation = -angle;
		recalculateTransform();
	}
	
	public void setFlipHorizontal(boolean value) {
		this.flipHorizontal = value;
		recalculateTransform();
	}
	
	public void setZoom(double zoom) {
		assert(zoom > 0.0);
		this.zoom = zoom;		
		recalculateTransform();
	}
	
	public double getZoom() {
		return this.zoom;
	}
	
	private void recalculateTransform() {
		this.transform.setToIdentity();
		this.transform.translate(
				this.viewport.getX() + this.viewport.getWidth() / 2.0, 
				this.viewport.getY() + this.viewport.getHeight() / 2.0);
		
		if (this.flipHorizontal) {
			this.transform.scale(this.zoom, -this.zoom);
		} else {
			this.transform.scale(this.zoom, this.zoom);
		}
		this.transform.rotate(-this.orientation);
		this.transform.translate(-this.posX, -this.posY);
	}

	public String getName() {
		return this.name;
	}
}
